package com.company;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @date : 2023-03-10 下午4:12
 * 两个线程调用，都会有自增
 */

public class P044_IncrementThreadUnsafe {

    private static final AtomicInteger INDEX = new AtomicInteger(1);

    public static void main(String[] args) {
        new Thread(() -> {
            try {
                Thread.sleep(500);
                INDEX.getAndIncrement();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();


        new Thread(() -> {
            try {
                Thread.sleep(1000);
                INDEX.getAndIncrement();
                System.out.println(INDEX.get());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }


}
